package com.car.carcontrol.base;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.car.carcontrol.AppManager;
import com.car.carcontrol.LoadingDialog;
import com.car.carcontrol.util.UIHelper;
import com.example.x6.serial.SerialPort;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class BaseFragment extends Fragment {
	protected LoadingDialog loadingDialog;
	protected Context context;

	public byte[] bytes = new byte[]{0x00,0x01,0x02,0x03,0x04,0x05};
	private SerialPort serialttyS1;
	public InputStream ttyS1InputStream;
	public OutputStream ttyS1OutputStream;

	public static int width = BaseApplication.width;
	public static int height = BaseApplication.height;
	public static float density = BaseApplication.density;

	public static int width_d = 246*width/1280;
	public static int height_d = 140*height/480;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//			width_d = 246*width/1280;
//			height_d = 140*height/480;
//		}else{
//			width_d = 0;
//			height_d = 0;
//		}

		Log.e("bf===0", Build.VERSION.SDK_INT+"==="+Build.VERSION_CODES.N+"==="+width_d+"==="+height_d);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		loadingDialog = new LoadingDialog(getActivity());
//		loadingDialog.setMessageText("数据加载...");
//		imageManager = new ImageManager(getActivity());
//		autoHelper = new LibraryHelper(getActivity());

		context = getActivity();
		BaseApplication.context = context;







//		init_serial();	//TODO
	}

	/* 打开串口 */
	private void init_serial() {
		try {
			serialttyS1 = new SerialPort(new File("/dev/ttyS1"),115200,0);
			ttyS1InputStream = serialttyS1.getInputStream();
			ttyS1OutputStream = serialttyS1.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void write(byte[] bytes){
		/* 串口发送字节 */
		try {
			ttyS1OutputStream.write(bytes);   //TODO
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void read(byte[] bytes){
		/* 串口发送字节 */
		try {
			int temp = ttyS1InputStream.read(bytes);   //TODO
//			tv_temp.setText(temp+"℃");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onStartCommon(final String title) {
		m_myHandler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null && !loadingDialog.isShowing()) {
					loadingDialog.setTitle(title);
					loadingDialog.show();
				}
			}
		});

	}

	public void onFailureCommon(final String s) {
		m_myHandler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null && loadingDialog.isShowing()){
					loadingDialog.dismiss();
				}
				UIHelper.ToastError(context, s);
			}
		});

	}

	Handler m_myHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message mes) {
			switch (mes.what) {
				case 0:
					break;
				default:
					break;
			}
			return false;
		}
	});

	public void finishMine() {
		AppManager.getAppManager().finishActivity(getActivity());
	}
}
